package com.photoeditor.demo.ad;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.TextViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.util.CubicBezierInterpolator;
import com.photoeditor.demo.util.FontUtil;
import com.wv.nw.ui.view.AllinoneAdView;

/**
 * Created by dell on 2017/12/18.
 */

public class EntranceAdView extends RelativeLayout {

    private RelativeLayout mTitleLayout;
    private TextView mTitleName;
    private FrameLayout mAdContent;
//    private ImageView mNoAdView;

    private @LayoutRes
    int mAdLayoutId = R.layout.ad_store_view;

    private AllinoneAdView mAllinoneAdView;
    private boolean isFristShow = false;

    public static EntranceAdView newEntranceAdView(@LayoutRes int layoutId, @Nullable ViewGroup root) {
        LayoutInflater inflater = LayoutInflater.from(ComponentContext.getContext());
        return (EntranceAdView) inflater.inflate(layoutId, root);
    }

    public static EntranceAdView newEntranceAdView(@Nullable ViewGroup root) {
        LayoutInflater inflater = LayoutInflater.from(ComponentContext.getContext());
        return (EntranceAdView) inflater.inflate(R.layout.item_entrance_ad, root);
    }

    public EntranceAdView(@NonNull Context context) {
        super(context);
    }

    public EntranceAdView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EntranceAdView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public EntranceAdView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mTitleLayout = findViewById(R.id.title_layout);
        mTitleName = findViewById(R.id.title_name);
        FontUtil.setCustomFont(mTitleName);
        mAdContent = findViewById(R.id.ad_content);
//        mNoAdView = findViewById(R.id.img_noad);
//        mNoAdView.setVisibility(GONE);
//        mNoAdView.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SVIP_ICON
//                        , StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_CLICK
//                        , StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SVIP_VALUE_ENTRANCE_AD);
//                SVipController.getInstance().showPurchaseActivity(SVipController.ENTRANCE_SVIP_ICON_AD);
//            }
//        });
    }

    public RelativeLayout getTitleLayout() {
        return mTitleLayout;
    }

    public RelativeLayout.LayoutParams getTitleLayoutParams() {
        return mTitleLayout != null ? (RelativeLayout.LayoutParams)mTitleLayout.getLayoutParams() : null;
    }

    public RelativeLayout.LayoutParams getAdContentLayoutParams() {
        return (RelativeLayout.LayoutParams)mAdContent.getLayoutParams();
    }

    public RelativeLayout.LayoutParams getNoAdLayoutParams() {
        return null;//(RelativeLayout.LayoutParams) mNoAdView.getLayoutParams();
    }

    public TextView getTitleName() {
        return mTitleName;
    }

    public void setTitleLayoutVisibility(int visibility) {
        if (mTitleLayout != null) {
            mTitleLayout.setVisibility(visibility);
        }
    }

    public void setTitleLayoutMargin(int left, int right, int top, int bottom) {
        if (mTitleLayout == null) {
            return;
        }
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mTitleLayout.getLayoutParams();
        if (left != -1) {
            lp.leftMargin = left;
        }
        if (right != -1) {
            lp.rightMargin = right;
        }
        if (top != -1) {
            lp.topMargin = top;
        }
        if (bottom != -1) {
            lp.bottomMargin = bottom;
        }
    }

    public void setAdContentMargin(int left, int right, int top, int bottom) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mAdContent.getLayoutParams();
        if (left != -1) {
            lp.leftMargin = left;
        }
        if (right != -1) {
            lp.rightMargin = right;
        }
        if (top != -1) {
            lp.topMargin = top;
        }
        if (bottom != -1) {
            lp.bottomMargin = bottom;
        }
    }

    public void setMargin(int left, int right, int top, int bottom, int middle) {
        RelativeLayout.LayoutParams titleLayoutLp = null;
        if (mTitleLayout != null) {
            //titleLayoutLp = (RelativeLayout.LayoutParams) mTitleLayout.getLayoutParams();
        }
        RelativeLayout.LayoutParams adContentLp = (RelativeLayout.LayoutParams) mAdContent.getLayoutParams();

        if (left != -1) {
            if (titleLayoutLp != null) {
                titleLayoutLp.leftMargin = left;
            }
            adContentLp.leftMargin = left;
        }
        if (right != -1) {
            if (titleLayoutLp != null) {
                titleLayoutLp.rightMargin = right;
            }
            adContentLp.rightMargin = right;
        }
        if (top != -1) {
            if (titleLayoutLp != null) {
                titleLayoutLp.topMargin = top;
            }
        }
        if (bottom != -1) {
            adContentLp.bottomMargin = bottom;
        }
        if (middle != -1 && titleLayoutLp != null) {
            titleLayoutLp.bottomMargin = middle;
        }
    }

    public void setAdLayoutId(@LayoutRes int layoutId) {
        this.mAdLayoutId = layoutId;
    }

    public boolean setData(@NonNull final AdItemBean bean) {
        if (bean.hasAd()) {
            mTitleLayout.setVisibility(VISIBLE);
            isFristShow = !bean.isIsShowed();
            bean.setIsShowed(true);
            mAdContent.removeAllViews();
            View adView = bean.getAdView();
            if (adView == null) {
                adView = bean.createAdView(mAdLayoutId);
            }
            TextView actionView = adView.findViewById(R.id.ad_btn_download);
            if (actionView != null) {
                TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
                        actionView
                        , 2
                        , 12
                        , 1
                        , TypedValue.COMPLEX_UNIT_SP);
            }

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            lp.gravity = Gravity.CENTER;
//            lp.topMargin = DeviceUtils.dip2px(getContext(), 8);
//            lp.bottomMargin = lp.topMargin;
            mAdContent.addView(adView, lp);
            return true;
        } else {
            bean.setOnAdRefreshListener(new AdItemBean.OnAdRefreshListener() {
                @Override
                public void onAdRefresh() {
                    setData(bean);
                    invalidate();
                    showLayoutAd();
                }
            });
            return false;
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        destroy();
    }

    public void destroy() {
        if (mAllinoneAdView != null) {
            mAllinoneAdView.destroy();
            mAllinoneAdView = null;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (isFristShow) {
            isFristShow = false;
            showLayoutAd();
        }
    }

    private void showLayoutAd() {
        CubicBezierInterpolator cubicBezierInterpolator = new CubicBezierInterpolator(0, 1.06, .73, 1);

        ViewCompat.setTranslationX(this, DeviceUtils.getScreenWidthPx());
        this.animate().translationX(0).setDuration(800).setInterpolator(cubicBezierInterpolator).start();
    }

    @Override
    public void setVisibility(int visibility) {
        if (Log.getStackTraceString(new Throwable()).contains("android.view.HwNsdImpl.blockAdView")) {
            return;
        }
        super.setVisibility(visibility);
    }
}
